package com.cskaoyan.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.springframework.stereotype.Component;

/**
 * @author stone
 * @date 2022/05/26 16:38
 */
@Component
public class CustomAspect {

    public void method1(JoinPoint joinPoint) {
        // 获得代理对象
        Object proxy = joinPoint.getThis();
        // 获得委托类对象
        Object target = joinPoint.getTarget();

        // 获得方法
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getName());
        // 获得参数
        Object[] args = joinPoint.getArgs();

        System.out.println("Before通知执行的方法");
    }

    public void after() {
        System.out.println("After通知执行方法");
    }

    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //委托类方法执行之前
        System.out.println("Around的前半部分");
        //委托类方法的调用

        /*Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
        }*/
        //Object proceed = joinPoint.proceed();
        //Object proceed = joinPoint.proceed(new Object[]{"songge"});
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length != 0) {
            args[0] = args[0] + " niupi";
        }
        Object proceed = joinPoint.proceed(args);

        //委托类方法执行之后
        System.out.println("Around的后半部分");


        return proceed;//返回值就是代理对象的执行结果
    }

    public void afterReturning(Object result) {
        System.out.println("AfterReturning接收到的结果：" + result);
    }

    public void afterThrowing(Exception exception) {
        System.out.println("AfterThrowing通知接收的异常：" + exception.getMessage());
    }
}
